package com.hzlj.position.config.controller;

import com.fz.common.base.dto.DeleteCommonDTO;
import com.fz.common.base.dto.PageParamDTO;
import com.fz.common.base.dto.QueryPageResultDTO;
import com.fz.common.base.dto.ResultDTO;
import com.fz.common.base.exception.PubException;
import com.fz.common.base.office.EasyExcelUtil;
import com.hzlj.position.config.common.dto.jzJbxxDeviceBind.JzJbxxDeviceBindQueryResultDTO;
import com.hzlj.position.config.common.dto.jzJbxxDeviceBind.JzJbxxDeviceBindSimpleQueryParamDTO;
import com.hzlj.position.config.common.dto.operatorActivation.*;
import com.hzlj.position.config.rest.operator.impl.MobileLocateRestLEV1;
import com.hzlj.position.config.service.JzJbxxDeviceBindService;
import com.hzlj.position.config.service.OperatorActivationService;
import com.hzlj.position.config.service.OperatorConfigService;
import com.hzlj.sqjz.system.service.JgService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 【定位-运营商开通】接口
 *
 * @author lifh
 * @date 2023-08-10 17:28:24
 */
@Slf4j
@RestController
@RequestMapping("/position/operator/activation")
public class OperatorActivationController {
    @Resource
    private JgService jgService;
    @Resource
    private OperatorActivationService operatorActivationService;
    @Resource
    private OperatorConfigService operatorConfigService;
    @Resource
    private MobileLocateRestLEV1 mobileLocateRestLEV1;
    @Resource
    private JzJbxxDeviceBindService jzJbxxDeviceBindService;

    /**
     * 通过id查询定位-运营商开通
     */
    @GetMapping("/{id}")
    public ResultDTO<OperatorActivationQueryResultDTO> getOperatorActivationById(@PathVariable("id") String id) {
        log.debug("【查询信息】定位-运营商开通:id={}", id);
        return new ResultDTO<>(operatorActivationService.getOperatorActivationById(id));
    }

    /**
     * 保存定位-运营商开通
     */
    @PutMapping("")
    public ResultDTO<OperatorActivationQueryResultDTO> saveOperatorActivation(@RequestBody OperatorActivationAddDTO dto) {
        log.info("【新增信息】定位-运营商开通={}", dto);
        operatorActivationService.saveOperatorActivation(dto);
        return new ResultDTO<>();
    }


    /**
     * 运营商开通/取消
     */
    @PostMapping(value = "/activation")
    public ResultDTO<Boolean> activation(@RequestBody OperatorActivationDTO dto) {
        log.info("【更新信息】定位-运营商开通/取消={}", dto);
        return new ResultDTO<>(operatorActivationService.activation(dto));
    }

    /**
     * 运营商开通/取消-批量
     */
    @PostMapping(value = "/activationBatch")
    public ResultDTO<Boolean> activationBatch(@RequestBody OperatorActivationBatchDTO dto) {
        JzJbxxDeviceBindSimpleQueryParamDTO query = new JzJbxxDeviceBindSimpleQueryParamDTO().setChannel(dto.getChannel());
        query.setJgCode(dto.getJgCode());
        List<JzJbxxDeviceBindQueryResultDTO> binds = jzJbxxDeviceBindService.listJzJbxxDeviceBind(query);
        for (JzJbxxDeviceBindQueryResultDTO bind : binds) {
            try {
                OperatorActivationQueryResultDTO exists = operatorActivationService.getOperatorActivationBySjh(bind.getDeviceCode());
                //新增
                if (exists == null) {
                    OperatorActivationAddDTO add = new OperatorActivationAddDTO()
                            .setJzId(bind.getJzId())
                            .setXm(bind.getXm())
                            .setSjh(bind.getDeviceCode())
                            .setJgCode(bind.getJgCode())
                            .setJgMc(bind.getJgMc())
                            .setChannel(bind.getChannel())
                            .setActivation(dto.getActivation());
                    operatorActivationService.saveOperatorActivation(add);
                } else {
                    OperatorActivationDTO active = new OperatorActivationDTO()
                            .setId(exists.getId())
                            .setJgCode(bind.getJgCode())
                            .setChannel(bind.getChannel())
                            .setSjh(bind.getDeviceCode())
                            .setActivation(dto.getActivation());
                    active.setUpdaterUid(dto.getUpdaterUid())
                            .setUpdaterName(dto.getUpdaterName())
                            .setUpdateRemark(dto.getUpdateRemark());
                    operatorActivationService.activation(active);
                }
            } catch (Exception e) {
                log.error("批量运营商开通/取消出错:jzId={},sjh={}", bind.getJzId(), bind.getDeviceCode(), e);
            }
        }
        return new ResultDTO<>(true);
    }


    /**
     * 直接取消运营商定位
     */
    @PostMapping(value = "/deactivateBatch")
    public ResultDTO<Boolean> deactivateBatch(@RequestBody OperatorActivationBatchByDeviceCodesDTO dto) {
        OperatorConfigService.OperatorTaskConfig operatorTaskConfig = operatorConfigService.taskConfig(dto.getJgCode(), dto.getChannel());
        OperatorActivationExtDTO ext = new OperatorActivationExtDTO();
        ext.setServiceId(dto.getServiceId());
        String[] deviceCodesSplit = dto.getDeviceCodes().split(",");
        for (String deviceCode : deviceCodesSplit) {
            try {
                this.mobileLocateRestLEV1.disablePosition(operatorTaskConfig.getConfig(), ext, dto.getJgCode(), deviceCode);
                log.info("运营商-取消成功：{}", deviceCode);
            } catch (PubException e) {
                log.error("运营商-取消失败：{}->{}", deviceCode, e.getMsg());
            } catch (Exception e) {
                log.error("运营商-取消失败：{}", deviceCode, e);
            }
        }
        return new ResultDTO<>(true);
    }


    /**
     * 删除定位-运营商开通
     */
    @DeleteMapping(value = "")
    public ResultDTO<Boolean> deleteOperatorActivation(@RequestBody DeleteCommonDTO<String> dto) {
        log.info("【删除信息】定位-运营商开通={}", dto);
        return new ResultDTO<>(operatorActivationService.deleteOperatorActivation(dto));
    }

    /**
     * 导出 定位-运营商开通
     */
    @GetMapping("/export")
    public void export(HttpServletResponse response,
                       OperatorActivationQueryParamDTO dto) {
        log.info("【导出无分页列表】信息定位-运营商开通,OperatorActivationQueryParamDTO={}", dto);
        List<OperatorActivationQueryResultDTO> list = operatorActivationService.listOperatorActivation(dto);
        EasyExcelUtil.downCustomerExcel(response,
                "白名单列表",
                "白名单列表",
                jgService.export3Level(OperatorActivationQueryResultDTO::getJgCode, OperatorActivationQueryResultDTO::getJgMc)
                        .add(OperatorActivationQueryResultDTO::getXm, "姓名")
                        .add(OperatorActivationQueryResultDTO::getChannelText, "运营商")
                        .add(OperatorActivationQueryResultDTO::getSjh, "手机号")
                        .add(OperatorActivationQueryResultDTO::getActivationText, "开通状态")
                        .add(OperatorActivationQueryResultDTO::getActivationTime, "开通时间")
                        .add(e -> e.getActivationDays() + "天", "累计时间")
                        .getFields(),
                list
        );
    }

    /**
     * 分页查询 定位-运营商开通
     */
    @GetMapping("/list/page")
    public ResultDTO<QueryPageResultDTO<OperatorActivationQueryResultDTO>> pageOperatorActivation(OperatorActivationQueryParamDTO dto, PageParamDTO pageParamDTO) {
        log.debug("【查询分页列表】信息定位-运营商开通,OperatorActivationQueryParamDTO={},pageParamDTO={}", dto, pageParamDTO);
        return new ResultDTO<>(operatorActivationService.pageOperatorActivation(dto, pageParamDTO));
    }
}

